package com.travel.travelnetwork.controller.back;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelnetwork.pojo.Admin;
import com.travel.travelnetwork.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.travel.travelnetwork.bean.RoleWithStatus;

import java.util.List;

@Controller
@RequestMapping("/back/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    //查询所有用户信息
    @RequestMapping("/all")
    @PreAuthorize("hasAnyAuthority('/admin/all')")
    public ModelAndView all(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "10")int size) {
        ModelAndView mav = new ModelAndView();
        Page<Admin> adminPage = adminService.findPage(page, size);
        mav.addObject("adminPage", adminPage);
        mav.setViewName("/back/admin_all");
        return mav;
    }
    //添加管理员
    @RequestMapping("/add")
    public String add(Admin admin) {
        adminService.add(admin);
        return "redirect:/back/admin/all";
    }
    //修改功能，通过id查询用户
    @RequestMapping("/edit")
    public ModelAndView add(Integer aid) {
        Admin admin = adminService.findbyid(aid);
        ModelAndView mav = new ModelAndView();
        mav.addObject("admin", admin);
        mav.setViewName("/back/admin_edit");
        return mav;
    }
    //修改管理员
    @RequestMapping("/updata")
    public String updata(Admin admin) {
        adminService.updata(admin);
        return "redirect:/back/admin/all";
    }

    //查询管理员详情
    @RequestMapping("/desc")
    public ModelAndView findDesc(Integer aid) {
        Admin admin =adminService.findDesc(aid);
        ModelAndView mav = new ModelAndView();
        mav.addObject("admin", admin);
        mav.setViewName("/back/admin_desc");
        return mav;
    }

    //管理员权限详情
    @RequestMapping("/findRole")
    public ModelAndView findRole(Integer aid) {
        List<RoleWithStatus> roles = adminService.findRole(aid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles", roles);
        modelAndView.addObject("aid", aid);
        modelAndView.setViewName("/back/admin_role");
        return modelAndView;

    }

    //修改管理员权限
    @RequestMapping("/updateRole")
    public String manageRole(Integer aid,Integer[] ids){
        adminService.updateRoles(aid,ids);
        return "redirect:/back/admin/all";
    }

    //更改管理员状态
    @RequestMapping("/updateStatus")
    public String manageStatus(Integer aid){
        adminService.upadtaStatus(aid);
        return "redirect:/back/admin/all";
    }
}
